﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using Ent = LivroBebe.Model;

namespace LivroBebe.Data
{
    public class Menu
    {
        public static List<Ent.Menu> CarregaMenu()
        {
            List<DBParametros> dbParametros = new List<DBParametros>();

            using (IDataReader reader = DBComum.ExecuteReader(dbParametros, "SP_MENU"))
            {
                return LeituraMenu(reader);
            }
        }

        public static List<Ent.MenuItem> CarregaMenuItem(int pId)
        {
            List<DBParametros> dbParametros = new List<DBParametros>();
            dbParametros.Add(new DBParametros() { Parametro = "@pId", Tipo = DbType.Int32, Valor = pId});

            using (IDataReader reader = DBComum.ExecuteReader(dbParametros, "SP_MENUITEM"))
            {
                return LeituraMenuItem(reader);
            }
        }

        public static List<Ent.MenuItemNivel> CarregaMenuItemNivel(int pIdMenu, int pIdMenuItem)
        {
            List<DBParametros> dbParametros = new List<DBParametros>();
            dbParametros.Add(new DBParametros() { Parametro = "@pId", Tipo = DbType.Int32, Valor = pIdMenu });
            dbParametros.Add(new DBParametros() { Parametro = "@pIdMenuItem", Tipo = DbType.Int32, Valor = pIdMenuItem });
            using (IDataReader reader = DBComum.ExecuteReader(dbParametros, "SP_MENUITEMNIVEL"))
            {
                return LeituraMenuItemNivel(reader);
            }
        }

        public static List<Ent.Menu> LeituraMenu(IDataReader reader)
        {
            List<Ent.Menu> ListaMenu = new List<Ent.Menu>();
            while (reader.Read())
            {
                ListaMenu.Add(new Ent.Menu() { IDMenuPai = (int)reader["ID"], Titulo = (string)reader["Titulo"], Url = (string)reader["Url"], MenuItem = CarregaMenuItem((int)reader["ID"]), TipoMenu = (int)reader["IDTipoMenu"], CSS = (string)reader["CSS"]});
            }
            return ListaMenu;
        }

        public static List<Ent.MenuItem> LeituraMenuItem(IDataReader reader)
        {
            List<Ent.MenuItem> MenuItens = new List<Ent.MenuItem>();
            while (reader.Read())
            {
                MenuItens.Add(new Ent.MenuItem() { IDItem = (int)reader["ID"], Titulo = (string)reader["Titulo"], Url = (string)reader["Url"], Imagem = (string)reader["Imagem"], MenuItemNivel = CarregaMenuItemNivel((int)reader["IDMenu"], (int)reader["ID"]) });
            }
            return MenuItens;
        }

        public static List<Ent.MenuItemNivel> LeituraMenuItemNivel(IDataReader reader)
        {
            List<Ent.MenuItemNivel> MenuItenNivel = new List<Ent.MenuItemNivel>();
            while (reader.Read())
            {
                MenuItenNivel.Add(new Ent.MenuItemNivel() { Titulo = (string)reader["Titulo"], Url = (string)reader["Url"] });
            }
            return MenuItenNivel;
        }
    }
}